package com.imdroid.carac.speechRecog;

import java.util.LinkedList;

import com.imdroid.carac.IMDroid;

import edu.cmu.pocketsphinx.Config;
import edu.cmu.pocketsphinx.Decoder;

class DecoderPool {

	
	
	

private static Config config;
private static LinkedList<Decoder> decoders = new LinkedList<Decoder>();

static {
//	pocketsphinx.setLogfile(IMDroid.SPEECH_RECOG_FOLDER+"ps.log");
	
	config = new Config();
//	config.setString("-rawlogdir", IMDroid.SPEECH_RECOG_FOLDER);
	config.setString( "-hmm", IMDroid.SPEECH_RECOG_FOLDER+"tdt_sc_8k" );
	config.setString( "-dict", IMDroid.SPEECH_RECOG_FOLDER+"A.dic" );
//	config.setString( "-lm", IMDroid.SPEECH_RECOG_FOLDER+"A.DMP" );
	config.setString( "-lm", IMDroid.SPEECH_RECOG_FOLDER+"A.arpa" );
	config.setFloat( "-samprate", SpeechRecogThread.SAMPLE_RATE );
	config.setInt( "-maxhmmpf", 2000 );
	config.setInt( "-maxwpf", 10 );
	config.setInt( "-pl_window", 2 );
	config.setBoolean( "-backtrace", true );
	config.setBoolean( "-bestpath", false );

	new Thread() { public void run() { threadRun(); } }.start();
}

static void init(){
}




static Decoder fetch() {
	synchronized( decoders ) {
		while( decoders.isEmpty() )
			try {
				decoders.wait();
			} catch( InterruptedException e ) {
				throw new RuntimeException( e );
			}
		Decoder d = decoders.removeFirst();
		decoders.notifyAll();
		return d;
	}
}

private static void threadRun() {
	while( true ) {
		Decoder decoder = new Decoder( config );
		synchronized( decoders ) {
			while( decoders.size() >= 2 )
				try {
					decoders.wait();
				} catch( InterruptedException e ) {
					throw new RuntimeException( e );
				}
			decoders.add( decoder );
			decoders.notifyAll();
		}
	}
}




}
